package com.bjpowernode.web.controller;


import com.bjpowernode.common.P2PConstants;
import com.bjpowernode.model.LoanInfo;
import com.bjpowernode.service.BidInfoService;
import com.bjpowernode.service.LoanInfoService;
import com.bjpowernode.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.List;

/**
 * 当前controller属于消费者
 */
@Slf4j
@Controller
public class IndexController {

    @DubboReference(interfaceClass = UserService.class)
    private UserService userService;

    @DubboReference(interfaceClass = BidInfoService.class)
    private BidInfoService bidInfoService;

    @DubboReference(interfaceClass = LoanInfoService.class)
    private LoanInfoService loanInfoService;

    //定义日志对象
   // private Logger logger= LoggerFactory.getLogger(IndexController.class);

    /*
    进入首页
     */
    @RequestMapping(value = {"/index","/"})
    public String index(Model model){

        //输出日志到文件
        //加了@Slf4j，直接在该类中可生成log日志对象
        log.debug("debug级别日志");
        log.info("info级别的日志");

        //调用dubbo的接口，获取注册用户数
        int users = userService.countRegistryUser();
        model.addAttribute("registryUsers",users);

        //累计成交金额
        BigDecimal sumBidMoney = bidInfoService.querySumBidMoney();
        model.addAttribute("sumBidMoney",sumBidMoney);

        //历史年化收益率
        BigDecimal historyAvgRate = loanInfoService.queryHistoryAvgTate();
        model.addAttribute("historyAvgRate",historyAvgRate);

        //查询一个新手宝的产品
        List<LoanInfo> XSBLoanInfoList = loanInfoService.queryLoanPageByProductType(
                P2PConstants.PRODUCT_TYPE_XINSHOUBAO_0, 1, 1);
        model.addAttribute("XSBLoanInfoList",XSBLoanInfoList);

        //优选宝的产品
        List<LoanInfo> YXLoanInfoList = loanInfoService.queryLoanPageByProductType(P2PConstants.PRODUCT_TYPE_YOUXUANBAO_1,1,4);
        model.addAttribute("YXLoanInfoList",YXLoanInfoList);

        //散标的产品
        List<LoanInfo> SBLoanInfoList = loanInfoService.queryLoanPageByProductType(P2PConstants.PRODUCT_TYPE_SANBIAOBAO_2,1,8);
        model.addAttribute("SBLoanInfoList",SBLoanInfoList);
        return "index";


    }
}
